package lock.lntelligence.gjx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.aiton.administrator.shane_library.shane.ZjbBaseActivity;

import java.util.ArrayList;
import java.util.List;

import lock.lntelligence.gjx.R;
import lock.lntelligence.gjx.constant.Constant;
import lock.lntelligence.gjx.model.BluetoothDeviceInfo;

public class ManagerUserActivity extends ZjbBaseActivity implements View.OnClickListener {
    private ListView mListView_device;
    private List<BluetoothDeviceInfo> mBluetoothDeviceList = new ArrayList<>();
    private MyAdapter mBluetoothAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_user);
        init();
    }

    @Override
    protected void initData() {
        //初始化一个数据
        mBluetoothDeviceList.add(new BluetoothDeviceInfo("龚建雄",  "C8:19:F7:9D:CC:3D"));
        mBluetoothDeviceList.add(new BluetoothDeviceInfo("祝志雄",  "38:BC:1A:B7:D9:3B"));
        mBluetoothDeviceList.add(new BluetoothDeviceInfo("张杰博",  "B6:1F:3C:A0:C8:D3"));
        mBluetoothDeviceList.add(new BluetoothDeviceInfo("屈明",  "E7:3B:23:34:F5:C3"));
        mBluetoothDeviceList.add(new BluetoothDeviceInfo("黄玉清",  "A6:1F:31:10:C2:B1"));
    }

    @Override
    protected void initViews() {
        mBluetoothAdapter = new MyAdapter();
        mListView_device.setAdapter(mBluetoothAdapter);
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageView_back).setOnClickListener(this);
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {

    }

    @Override
    protected void findID() {
        mListView_device = (ListView) findViewById(R.id.listView_device);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.imageView_back:
                finishTo();
                break;
        }
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mBluetoothDeviceList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = getLayoutInflater().inflate(R.layout.lisetitem_device, null);
            TextView textView_deviceName = (TextView) inflate.findViewById(R.id.textView_deviceName);
            TextView textView_deviceAdd = (TextView) inflate.findViewById(R.id.textView_deviceAdd);
            Button button_do = (Button) inflate.findViewById(R.id.button_do);
            button_do.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(ManagerUserActivity.this);
                    View userDo = getLayoutInflater().inflate(R.layout.user_do, null);
                    builder.setView(userDo);
                    final AlertDialog alertDialog = builder.create();
                    alertDialog.show();
                    userDo.findViewById(R.id.textView_one).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(ManagerUserActivity.this, "允许用户使用一次", Toast.LENGTH_SHORT).show();
                            alertDialog.dismiss();
                        }
                    });
                    userDo.findViewById(R.id.textView_forever).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(ManagerUserActivity.this, "允许用户永久使用", Toast.LENGTH_SHORT).show();
                            alertDialog.dismiss();
                        }
                    });
                    userDo.findViewById(R.id.textView_cancle).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(ManagerUserActivity.this, "取消用户操作权限", Toast.LENGTH_SHORT).show();
                            alertDialog.dismiss();
                        }
                    });
                }
            });
            if (mBluetoothDeviceList.size() > 0) {
                textView_deviceName.setText(mBluetoothDeviceList.get(position).getBluetoothName());
                textView_deviceAdd.setText(mBluetoothDeviceList.get(position).getBluetoothAddress());
            }
            return inflate;
        }
    }

    private boolean isSleep =false;

    @Override
    protected void onStart() {
        super.onStart();
        if (isSleep){
            Intent intent = new Intent();
            intent.setClass(ManagerUserActivity.this,LockActivity.class);
            intent.putExtra("sleep","sleep");
            startActivityForResultTo(intent, Constant.REQUEST_RESULT.IS_SLEEP);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        isSleep=true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == Constant.REQUEST_RESULT.IS_SLEEP && resultCode == Constant.REQUEST_RESULT.IS_SLEEP){
            isSleep = false;
        }
    }
}
